//
//  HMComposePictureView.swift
//  Weibo
//
//  Created by Apple on 2017/7/16.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit

class HMComposePictureView: UICollectionView {

    var callBack: (()->())?
    
    //  图片数据源
    lazy var images: [UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layout = UICollectionViewFlowLayout()
        let itemMargin: CGFloat = 5
        let itemWidth = (ScreenWidth - 2 * HomeCellMargin - 2 * itemMargin) / 3
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        layout.minimumLineSpacing = itemMargin
        layout.minimumInteritemSpacing = itemMargin
        super.init(frame: frame, collectionViewLayout: layout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        register(HMComposePictureViewCell.self, forCellWithReuseIdentifier: ComposePictureCellIdentifier)
        dataSource = self
        delegate = self
        self.isHidden = true
    }
    
    //  添加图片
    func addImage(image: UIImage) {
        if images.count >= 9 {
            return
        }
        self.isHidden = false
        images.append(image)
        reloadData()
    }
    
}

extension HMComposePictureView: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if images.count == 0 || images.count == 9 {
            return images.count
        }
        return images.count + 1
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ComposePictureCellIdentifier, for: indexPath) as! HMComposePictureViewCell
        if indexPath.item == images.count {
            cell.image = nil
        } else {
            cell.image = images[indexPath.item]
            cell.deleteCallBack = { [unowned self] (index) in
                self.images.remove(at: index)
                self.reloadData()
                if self.images.count == 0 {
                    self.isHidden = true
                }
            }
        }
        cell.indexPath = indexPath
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == images.count {
            //  最后一个cell
            callBack?()
        }
    }
}

//  自定义撰写微博配图cell
class HMComposePictureViewCell: UICollectionViewCell {
    
    var indexPath: IndexPath?
    
    var deleteCallBack: ((Int)->())?
    
    fileprivate lazy var imageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
    fileprivate lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(deleteButtonAction), for: .touchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), for: .normal)
        return button
    }()
    
    var image: UIImage? {
        didSet {
            if image == nil {
                imageView.image = UIImage(named: "compose_pic_add")
                deleteButton.isHidden = true
            } else {
                imageView.image = image
                deleteButton.isHidden = false
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(imageView)
        contentView.addSubview(deleteButton)
        imageView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        deleteButton.snp.makeConstraints { (make) in
            make.trailing.equalTo(imageView)
            make.top.equalTo(imageView)
        }
    }
    
    //  MARK:   --删除图片按钮点击事件
    @objc private func deleteButtonAction() {
        if let currentIndexPath = indexPath {
            deleteCallBack?(currentIndexPath.item)
        }
    }
}
